
var System = {
	getRootPath:function(){
		if(path){
			return path;
		}
		var curWwwPath=window.document.location.href;
		var pathName=window.document.location.pathname;
		var pos=curWwwPath.indexOf(pathName);
		var localhostPaht=curWwwPath.substring(0,pos);
		var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
		path = localhostPaht+projectName;
		return path;
	},
	getRequest:function(name,url) {
		if(!url){
			url = location.search;
		}
	    var result = url.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
	    if (result == null || result.length < 1) {
	        return "";
	    }
	    return result[1];
	},
	getCndRequests:function(url) {
		if(!url){
			url = location.search;
		}
		var result = url.match(new RegExp("[\?\&]cnd_([^\&]+)=([^\&]+)", "ig"));
		if (result == null || result.length < 1) {
	        return "";
	    }
		var parameters = new Array();
		for(var i=0;i<result.length;i++){
			var p1 = result[i];
			p1 = p1.substring(1);
			p1 = p1.split("=");
			parameters[i] = p1;
		}
		return parameters;
	},
	parseDict:function(dics,dicId){
		if(!dics){
			return '';
		}
		var dicValue = "";
		$.each(dics,function(i,dic){
			if(dic.key==dicId){
				dicValue = dic.value;
			}
		});
		return dicValue;
	},
	ajaxPost:function(url,data,callFunc,async,dataType){
		if(data===undefined){
			data = '';
		}
		if(async===undefined){
			async = true;
		}
		if(dataType===undefined){
			dataType='json';
		}
		$.ajax({
            url: url,
            type: 'POST',
            data:data,
            dataType: dataType,
			async:async,
            timeout: 1000,
            error: function () {
            },
            success: function (data) {
            	if(!data.result){
            		System.showError('错误', data.msg);
            	}else{
            		if(typeof callFunc =='function'){
            			callFunc(data.data);
            		}
            	}
            	if(data.isRedirect==1&&data.urlPath){
            		System.redirect(data.urlPath);
            	}
            }
        });
	},
	ajaxGet:function(url,callFunc,async,dataType){
		if(async===undefined){
			async = true;
		}
		if(dataType===undefined){
			dataType='json';
		}
		$.ajax({
            url: url,
            type: 'get',
            dataType: dataType,
			async:async,
            error: function () {
            },
            success: function (data) {
            	if(!data.result){
            		System.showError('错误', data.msg);
            	}else{
            		if(data.msg){
            			System.showError('提示', data.msg);
            		}
            		if(typeof callFunc =='function'){
            			callFunc(data.data);
            		}
            	}
            	if(data.idRedirect==1&&data.urlPath){
            		System.redirect(data.urlPath);
            	}
            }
        });
	},
	ajax:function(type,url,data,callFunc,async,dataType){
		if(type===undefined){
			type = 'post';
		}
		if(type=='get'){
			System.ajaxGet(url, callFunc, async, dataType);
		}else{
			System.ajaxPost(url, data, callFunc, async, dataType);
		}
	},
	showError:function(title,msg){
		if(!title){
			title = "错误";
		}
		top.$.messager.alert({
			title:title,
			msg:msg,
			width:500
		});
	},
	showMsg:function(msg,title,icon){
		if(!title){
			title = "提示";
		}
		if(!icon){
			icon = "info";
		}
		$.messager.alert({
	        title: title,
	        msg: msg,
	        icon:icon
	    });
	},
	replaceParam:function(sourceStr,paramFields,data){
		if(!paramFields){
			return sourceStr;
		}
		var params = paramFields.split(",");
		var pIndex = 0;
		while(sourceStr.indexOf('|*|')>=0&&pIndex<params.length){
			var param = params[pIndex];
			var repValue = data[param];
			sourceStr = sourceStr.replace("|*|",repValue);
			pIndex++;
		}
		return sourceStr;
	},
	getPageUrl:function(url){
		if(!url){
			return '';
		}
		var i = url.lastIndexOf("/");
		var n = url.indexOf("?");
		var mvcAction = "";
		if(n==-1){
			mvcAction = url.substring(i + 1, url.length - 3);
		}else{
			mvcAction = url.substring(i + 1, n - 3);
		}
		var pageUrl = "";
		var getPageUrl = System.getRootPath()+"/console.actionPage.do";
		System.ajaxPost(getPageUrl, "mvcAction="+mvcAction, function(data){
			if(data.type==3){//ajax
				pageUrl = "ajax";
			}else if(data.page){
				pageUrl = System.getRootPath()+data.page;
			}else{
				System.showError('错误', "页面不存在!");
			}
		},false);
		if(!pageUrl){
			return pageUrl;
		}
		/*
		if(mvcAction=='sysmng.initInsert'){
			pageUrl = "/page/syspub/dbop/add.jsp";
		}else if(mvcAction=='sysmng.initUpdate'){
			pageUrl = "/page/syspub/dbop/edit.jsp";
		}else if(mvcAction=='sysmng.report'){
			pageUrl = "/page/syspub/report/report.jsp";
		}else if(mvcAction=='sysmng.delete'){
			return pageUrl = "del";
		}else if(mvcAction=='sysmng.colInfoGuide'){
			pageUrl = "/page/console/syspub/selTbls.jsp";
		}
		*/
		return pageUrl;
	},
	redirect:function(url){
		var pageUrl = System.getPageUrl(url);
		var callerPageURL = location.href;
		location.href=pageUrl+'?funcUrl='+encodeURIComponent(url)+"&callerPageURL="+encodeURIComponent(callerPageURL);
	},
	openDialog:function(title,href,width,height,sourceObj){
		if(!width){
			width = 500;
		}
		if(!height){
			height = 380;
		}
		var t = new Date().getTime();
		var dlgId = 'Dlg_'+t;
		var dlgDiv = "<div id='"+dlgId+"'></div>";
		top.$("body").append(dlgDiv);
		if(href.indexOf("?")>0){
			href += "&dlgId="+dlgId;
		}
		var dialog = top.$('#'+dlgId).dialog({
			title:title,
			closed: false,
			modal: true,
			width: width,    
			height: height,
			href:href,
			isIframe:true,
			onClose:function(){
				System.delCache(dlgId);
			}
		});
		if(sourceObj){
			dialog.sourceObj = sourceObj;
		}
		System.setCache(dlgId, dialog);
		return dlgId;
	},
	/**
	 * 弹出窗口设置字典量,采用系统内定的URL处理.
	 * @param subSys 子系统标识.
	 * @param dicKind 字典类型.
	 * @param dicGrpID 字典组标识.
	 * @param valDicCol 值对应字典量列.
	 * @param imgObj 图片对象，id为:pop_对应输入元素的ID.
	 */
	popSetDic:function(subSys,dicKind,dicGrpID,valDicCol,popInputId){
//		var url = "sysmng.popSetDic.do?subSys="+subSys+"&dicKind="+dicKind+"&dicGrpID="+dicGrpID+"&valDicCol="+valDicCol;
		var url = System.getRootPath()+"/page/syspub/popdic/popDic.jsp?subSys="+subSys+"&dicKind="+dicKind+"&dicGrpID="+dicGrpID+"&valDicCol="+valDicCol+"&popInputId="+popInputId;
		var curDlgId = System.getRequest("dlgId",location.href);
		if(curDlgId){
			url = url+"&pDlgId="+curDlgId;
		}
		System.openDialog('选择数据项', url, 500, 380);
	},
	setCache:function(key, value){
		top.AppCache.set(key, value);
	},
	getCache:function(key){
		return top.AppCache.get(key);
	},
	delCache:function(key){
		top.AppCache.del(key);
	}
}